package ue.client.core;

import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Image;
import org.newdawn.slick.TrueTypeFont;

import ue.client.networking.Client;
import ue.client.planes.Subspace;

public class Game {
	
	public static Client client;
	
	public static final int WIDTH = Display.getDesktopDisplayMode().getWidth();
	public static final int HEIGHT = Display.getDesktopDisplayMode().getHeight();
	
    public static TrueTypeFont font;
    
	public static void main(String[] args) {
		LoginWindow login = new LoginWindow();
		
		while(client == null || !client.isIdentified()) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		login.dispose();
		
		try {
			Display.setDisplayMode(Display.getDesktopDisplayMode());
			Display.setTitle("Unto Eternity");
			Display.setFullscreen(false);
			Display.setResizable(false);
			Display.create();
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
		
		//Initialize OpenGL
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        
        init();
		
		while(!Display.isCloseRequested()) {
			
			Subspace.tick();
			
			Display.update();
			clean();
			
			Subspace.render();
			
			Display.sync(60);
			
		}
		
		System.out.println(Game.getTimestamp() + "Disconnected.");
		
		Display.destroy();
		System.exit(0);
	}
	
	public static void init() {
		font = new TrueTypeFont(new Font("Arial", Font.PLAIN, 16), true);
		
		Subspace.init();
	}
	
	public static void drawTexture(Image image, int x, int y, int width, int height, int angle) {
		image.getTexture().bind();
		
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
        
		GL11.glColor3f(1.0f, 1.0f, 1.0f);
		GL11.glLoadIdentity();
		final float n = 3.18f;
		GL11.glTranslatef(x + width / n, y + height / n, 0.0f);
		GL11.glRotatef(angle, 0.0f, 0.0f, 1.0f);
		GL11.glTranslatef(-(x + width / n), -(y + height / n), 0.0f);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
        
		GL11.glBegin(GL11.GL_QUADS);
			GL11.glTexCoord2f(0, 0);
			GL11.glVertex2f(x, y);
			GL11.glTexCoord2f(1, 0);
			GL11.glVertex2f(x + width, y);
			GL11.glTexCoord2f(1, 1);
			GL11.glVertex2f(x + width, y + height);
			GL11.glTexCoord2f(0, 1);
			GL11.glVertex2f(x, y + height);
		GL11.glEnd();
	}
	
	public static void drawLine(float x1, float y1, float x2, float y2, float[] color) {
		GL11.glColor4f(color[0], color[1], color[2], color[3]);
		GL11.glLoadIdentity();
		
		GL11.glBegin(GL11.GL_LINES);
			GL11.glVertex2f(x1, y1);
			GL11.glVertex2f(x2, y2);
		GL11.glEnd();
	}
	
	public static void drawRectangle(float x1, float y1, float x2, float y2, float[] color) {
		GL11.glColor4f(color[0], color[1], color[2], color[3]);
		GL11.glLoadIdentity();
		
		GL11.glBegin(GL11.GL_QUADS);
			GL11.glVertex2f(x1, y1);
			GL11.glVertex2f(x2, y1);
			GL11.glVertex2f(x2, y2);
			GL11.glVertex2f(x1, y2);
		GL11.glEnd();
	}
	
	public static void drawEmptyRectangle(float x1, float y1, float x2, float y2, float[] color) {
		GL11.glColor4f(color[0], color[1], color[2], color[3]);
		GL11.glLoadIdentity();
		
		GL11.glBegin(GL11.GL_LINE_LOOP);
			GL11.glVertex2f(x1, y1);
			GL11.glVertex2f(x2, y1);
			GL11.glVertex2f(x2, y2);
			GL11.glVertex2f(x1, y2);
		GL11.glEnd();
		
	}
	
	public static void clean() {
		drawRectangle(0, 0, WIDTH, HEIGHT, new float[] { 0.0f, 0.0f, 0.0f, 1.0f });
	}
	
	public static String getTimestamp() {
		return "[" + new SimpleDateFormat("yyyy.MM.dd:HH.mm.ss.SSS").format(new Date()) + "]";
	}
}
